package org.fanfan.struts.actions;

import java.io.File;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.fanfan.struts.actionForms.UploadForm;

public class UploadAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UploadForm uploadForm = (UploadForm) form;
		FormFile file = uploadForm.getFile();
		String contentType = file.getContentType();
		String fileName = file.getFileName();
		int fileSize = file.getFileSize();
		byte[] fileData = file.getFileData();
		System.out.println("contentType:"+contentType);
		System.out.println("fileName:"+fileName);
		System.out.println("fileSize:"+fileSize);
		
		FileOutputStream fos = new FileOutputStream(new File("d:\\"+fileName));
		fos.write(fileData);
		fos.close();
		
		HttpSession session = request.getSession();
		session.setAttribute("contentType", contentType);
		session.setAttribute("fileName", fileName);
		session.setAttribute("fileSize", fileSize);
		
		return mapping.findForward("download");
	}

}
